﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using NIF.MobileActors.Core;

namespace NIF.MobileActors.Hosts
{
    public class HostRemotingFacade : MarshalByRefObject, IHostRemoting
    {
        //public const string RemotingURI = "NIF.MobileActors.Hosts.RemotingHost";

        public bool Understand(string asmName)
        {
            return AssemblyManager.Instance.IsLocalAssembly(asmName);
        }

        public byte[] RetrieveAssemblyBytes(string asmName)
        {
            return AssemblyManager.Instance.GetRawAssembly(asmName);
        }

        public void DispatchTo(IActor actor)
        {
            if (actor != null)
            {
                IActor act = ActorManager.Instance.GetActor(actor.ID);
                if (act != null)
                {
                    act.ResetForExecute(actor);
                    actor = act;
                }
                else
                {
                    ActorManager.Instance.Add(actor.ID, actor);
                    actor.ResetForExecute();
                }
                actor.Activate();
                actor.OnDispatched();

                Dispatcher.Instance.ReadyToExecute(actor);
            }
        }

        public void DispatchAssemblyTo(string name, byte[] asmCodes)
        {
            AssemblyManager.Instance.Add(name, asmCodes);
        }

        //public IActor Retract(ActorID id)
        //{
        //    throw new NotImplementedException();
        //}

        //public ActorID Clone(IActor actor, string actorTypeName)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
